package com.system.service.mail.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.constant.HTTPStatus;
import com.system.entity.mail.MailRecord;
import com.system.entity.monitor.logs.SysLogininfor;
import com.system.mapper.mail.MailRecordMapper;
import com.system.response.Result;
import com.system.response.mail.MailRecordDtoResponse;
import com.system.service.mail.MailRecordService;
import com.system.util.StringUtils;
import lombok.Data;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Classname MailRecordServiceImpl
 * @Date 2023/8/11 9:55
 * @Created ZFC
 */
@Service
@Log4j
public class MailRecordServiceImpl extends ServiceImpl<MailRecordMapper, MailRecord> implements MailRecordService {
    @Autowired
    private MailRecordMapper mailRecordMapper;


    /**
     * 添加邮件记录
     *
     * @param mailRecord
     */
    @Override
    public void saveMailRecord(MailRecord mailRecord) {
        mailRecord.setSendTime(LocalDateTime.now());
        mailRecordMapper.insert(mailRecord);
    }

    /**
     * 条件查询邮件列表
     *
     * @param mailRecordDto
     * @return
     */
    @Override
    public Result mailList(MailRecordDtoResponse mailRecordDto) {

        if (mailRecordDto.getPageSize()==null || mailRecordDto.getPageSize()<0) mailRecordDto.setPageSize(1);
        if (mailRecordDto.getPageNum()==null || mailRecordDto.getPageNum()<10) mailRecordDto.setPageNum(10);

        PageHelper.startPage(mailRecordDto.getPageSize(),mailRecordDto.getPageNum());

        List<MailRecord> mailRecords = mailRecordMapper.selectList(
                new LambdaQueryWrapper<MailRecord>()
                        .like(StringUtils.isNotNull(mailRecordDto.getRecipientsMail()), MailRecord::getRecipientsMail, mailRecordDto.getRecipientsMail())
                        .like(StringUtils.isNotNull(mailRecordDto.getSubject()), MailRecord::getSubject, mailRecordDto.getSubject())
                        .eq(StringUtils.isNotNull(mailRecordDto.getStatus()), MailRecord::getStatus, mailRecordDto.getStatus())
                        .ge(StringUtils.isNotEmpty(mailRecordDto.getStartTime()), MailRecord::getSendTime, mailRecordDto.getStartTime())
                        .le(StringUtils.isNotEmpty(mailRecordDto.getEndTime()), MailRecord::getSendTime, mailRecordDto.getEndTime())
                        .orderByDesc(MailRecord::getSendTime)
        );
        Integer count = mailRecordMapper.selectCount(new LambdaQueryWrapper<>());
        PageInfo pageInfo = new PageInfo(mailRecords);
        pageInfo.setSize(count);


        return new Result().setCode(HTTPStatus.SUCCESS).setData(pageInfo);
    }

    /**
     * 批量删除邮件记录
     *
     * @param ids
     * @return
     */
    @Override
    public Result deleteMailRecord(Integer[] ids) {

        List<Integer> collect = Arrays.stream(ids).collect(Collectors.toList());

        mailRecordMapper.deleteBatchIds(collect);

        return new Result().setCode(HTTPStatus.SUCCESS).setMessage("删除成功!");
    }

    /**
     * 清空邮件记录
     *
     * @return
     */
    @Override
    public Result clearMailRecord() {
        mailRecordMapper.delete(new LambdaQueryWrapper<>());
        return new Result().setCode(HTTPStatus.SUCCESS).setMessage("清空成功！");
    }
}
